package com.datar.compute.model.syyjs;

import com.aoip.telemetry.telemetry.core.redis.RedisCache;
import com.aoip.telemetry.telemetry.entity.constant.ComputeModelConstants;
import com.aoip.telemetry.telemetry.entity.enums.CacheKeyEnum;
import com.aoip.telemetry.telemetry.entity.quality.ComputeModelEntity;
import com.aoip.telemetry.telemetry.entity.waga.WagaDefinitionEntity;
import com.aoip.telemetry.telemetry.entity.ysq.StRsvrEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.datar.compute.model.syyjs.mapper.SyyStRsvrMapper;
import com.datar.compute.model.syyjs.mapper.SyyWagaInfoMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 计算模型--实用堰计算
 *
 * @author chenbai
 * @version 1.0
 * @date 2022/4/30 16:07
 */
@Component
public class SyyjsModel {

    @Resource
    private SyyStRsvrMapper mapper;

    @Resource
    private SyyWagaInfoMapper syyWagaInfoMapper;

    @Resource
    private RedisCache redisCache;

    public void computeModel() {
        Collection<String> keys = redisCache.keys(CacheKeyEnum.COMPUTE_MODEL + ":*");
        List<String> stCodeList = new ArrayList<>();
        keys.forEach(key -> {
            ComputeModelEntity entity = redisCache.getCacheObject(key);
            String modelId = entity.getId();
            if ("1520755898066628609".equals(modelId)) {
                List<String> cacheList = redisCache.getCacheList(CacheKeyEnum.REL_COMPUTE_MODEL + ":" + modelId);
                if (cacheList != null) {
                    stCodeList.addAll(cacheList);
                }
            }
        });
        if (stCodeList.size() < 1) {
            return;
        }
        LocalDateTime dateTime = redisCache.getCacheObject(CacheKeyEnum.COMPUTE_MODEL + "_syy_time");
        LocalDateTime now = LocalDateTime.now();
        if (dateTime == null) {
            dateTime = now.minus(30, ChronoUnit.MINUTES);
            redisCache.setCacheObject(CacheKeyEnum.COMPUTE_MODEL + "_kdy_time", now);
        }
//        LocalDateTime dateTime = LocalDateTime.of(2022, 2, 16, 20, 0);
        QueryWrapper<StRsvrEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(true, "TM", dateTime);
        queryWrapper.in(true, "stcd", stCodeList);
        List<StRsvrEntity> stRsvrList = mapper.selectList(queryWrapper);
        QueryWrapper<WagaDefinitionEntity> wagaQueryWrapper = new QueryWrapper<>();
        wagaQueryWrapper.in(true, "waga_code", stCodeList);
        List<WagaDefinitionEntity> wagaInfoList = syyWagaInfoMapper.selectList(wagaQueryWrapper);
        if (stRsvrList == null || stRsvrList.size() < 1) {
            return;
        }
        if (wagaInfoList == null || wagaInfoList.size() < 1) {
            return;
        }
        for (StRsvrEntity stRsvr : stRsvrList) {
            wagaInfoList.forEach(entity -> {
                Double wagaPssw = stRsvr.getRz();
                Double wagaYdgc = entity.getWagaYdgc();
                Double wagaXysw = stRsvr.getBlrz();
//        闸孔数
                Integer wagaZks = entity.getWagaZks();
//        堰宽（单孔）B(m)
                Double wagaYkdk = entity.getWagaYkdk();
                //        堰静水头H(m)
                Double yjst = wagaPssw - wagaYdgc;
                yjst = Double.valueOf(String.format("%.2f", yjst));
//        Ho3/2
                double yjstH0 = Math.pow(yjst, 1.5);
                yjstH0 = Double.valueOf(String.format("%.4f", yjstH0));
//        流量系数m
                double llxs = 0.39;
//        hs/H0 =IF((F3-D3)/I3<0,0,(F3-D3)/I3)
                double hsH0 = 0.0;
                Double hs = wagaXysw - wagaYdgc;
                if (hs < 0) {
                    hsH0 = 0.0;
                } else {
                    hsH0 = hs / yjst;
                }
//        淹没系数δ
                double hsH0te = hsH0 / 0.05;

                double hsH0temp = Math.floor(hsH0te);
                double ymxs = 0.0;
                if (hsH0temp >= 20) {
                    ymxs = 0.0;
                } else {
                    ymxs = 1.0;
                }
//        边墩系数
                double bdxs = 1.0;
//        闸墩系数
                double zdxs = 0.8;
//        侧收缩系数ε
                double cssxs = 1 - 0.2 * (bdxs + (wagaZks - 1) * zdxs) * yjst / (wagaZks * wagaYkdk);
//        计算流量
                double jsll = cssxs * llxs * ymxs * wagaYkdk * Math.pow(2 * ComputeModelConstants.G, 0.5) * yjstH0 * wagaZks;
                System.out.println("计算流量" + jsll);
                stRsvr.setInq(jsll);
                UpdateWrapper<StRsvrEntity> updateWrapper = new UpdateWrapper<>();
                String stcd = stRsvr.getStcd();
                LocalDateTime tm = stRsvr.getTm();
                updateWrapper.eq(true, "TM", tm);
                updateWrapper.eq(true, "stcd", stcd);
                mapper.update(stRsvr, updateWrapper);
            });
        }

    }

    public static void main(String[] args) {
        double l = 0.69;
        double pow = Math.pow(l, 1.5);
        System.out.println(pow);
        System.out.println(Math.floor(0.69));

    }
}
